package com.wjm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wjm.entity.Admin;
import com.wjm.mapper.interf.AdminMapper;
import com.wjm.service.interf.AdminServiceInterface;

@Service("adminService")
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class) // 注明需要回滚
public class AdminServiceImpl implements AdminServiceInterface {

	@Autowired
	private AdminMapper adminMapper;

	@Override
	public boolean save(Admin object) throws Exception {
		boolean result = false;
		if (adminMapper.save(object) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public boolean delete(int objectId) throws Exception {
		boolean result = false;
		if (adminMapper.delete(objectId) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public boolean update(Admin object) throws Exception {
		boolean result = false;
		if (object.getAdminId() > 0 && adminMapper.update(object) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public Admin login(String adminCode, String adminPass) throws Exception {
		Admin admin = null;
		if (adminCode != null && !adminCode.equals("")) {
			admin = adminMapper.selectByCode(adminCode);
			if (admin != null && admin.getAdminPass().equals(adminPass) && admin.getAdminStatus() == 1) {
				return admin;
			}
		}
		return null;
	}

	@Override
	public boolean updateStatus(int adminId, int status) throws Exception {
		boolean result = false;
		if (adminId > 0 && (status == 1 || status == 2) && adminMapper.updateStatus(adminId, status) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public List<Admin> getByStatus(int status) throws Exception {
		return adminMapper.selectByStatus(status);
	}

	
	
}
